package labox.innovation.gameserver.model;

import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.clientpackets.RequestForgeItem;
import labox.innovation.gameserver.templates.item.ItemStarFactorConfig;
import labox.innovation.gameserver.templates.item.ItemStarManger;
import labox.innovation.gameserver.templates.item.L2Item;

/**
 * 物品 锻造 链表
 * 保存物品锻造时 的物品
 * @author zjj
 *
 */
public class ForgeItemList {

	public static final int FORGE_ITEM_KEEP = 0; //保底符

	public static final int FORGE_ITEM_CHANCE = 1; //幸运符

	public class ForgeItem {

		public long _objectId;

		public final L2Item _item;

		public int _count;

		public int _iconid;

		public int _itemType; //物品类型 .  0:保底符， 1:幸运符

		public ForgeItem(L2ItemInstance item, int count) {
			_objectId = item.getObjectId();
			_item = item.getItem();
			_count = count;
			_iconid = item.getItem().getItemIconId();

		}

		public ForgeItem(L2ItemInstance item, int count, int itemType) {
			_objectId = item.getObjectId();
			_item = item.getItem();
			_count = count;
			_iconid = item.getItem().getItemIconId();
			_itemType = itemType;

		}

	}

	//物品的锻造链表
	private final FuzePcInstance _owner;

	private ForgeItem[] _items;

	private int _limitSize; //辅助物品的最大个数

	private int _currentSize; //当前的数量

	private int _flag; //锻造连的类型   0:鉴定 1:铭刻2:追星3:合成

	private long _mainItemId; //

	public ForgeItemList(FuzePcInstance owner, int flag, int size) {
		_owner = owner;
		_flag = flag;
		_limitSize = size;
		_currentSize = 0;
		if (_limitSize > 0) {
			_items = new ForgeItem[_limitSize];
		}

	}

	public int getForgeType() {
		return _flag;
	}

	public void setMainItemId(long id) {
		_mainItemId = id;
	}

	public long getMainItemId() {
		return _mainItemId;
	}

	public ForgeItem[] getItems() {
		return _items;
	}

	public long[] getItemIds() {
		if( _currentSize <= 0 ){
			return null;
		}
		long[] idList = new long[_currentSize];
		int i = 0;
		for (ForgeItem item : _items) {
			if (item != null) {
				idList[i] = item._objectId;
				i++;
			}
		}

		return idList;
	}

	public void clear() {
		_currentSize = 0;
		if (_limitSize > 0) {
			_items = new ForgeItem[_limitSize];
		}

	}

	/**
	 * 获得 追星的 保底符道具Id
	 * @return
	 */
	public long getKeepItemId() {
		return _items[1] != null ? _items[1]._objectId : 0;
	}

	/**
	 * 获得 追星的幸运符道具Id
	 * @return
	 */
	public long getChanceItemId() {
		return _items[0] != null ? _items[0]._objectId : 0;
	}

	/**
	 * 往锻造链表中 增加物品
	 * @param objectId   物品uid
	 * @param count      物品数量
	 * @return  新增的 物品
	 */
	public ForgeItem addItem(long objectId, int count) {
		if (_currentSize >= _limitSize) {
			return null;
		}

		L2ItemInstance item = _owner.getInventory().getItemByObjectId(objectId);
		if (item == null) {
//			System.out.println("物品不存在");
			return null;
		}

		if (hasItem(objectId)) {
			return null;
		}

//		System.out.println("增加物品 物品id:" + objectId + "物品数量" + count);
		
		ForgeItem forgeItem = null;
		//追星要特殊处理. 第1个物品时是幸运符， 第2个是保底符
		if (_flag == RequestForgeItem.OP_START) {
//			System.out.println("处理追星数据");
			if (ItemStarFactorConfig.getInstance().isInclude(item.getItemId())) { //保底符
				forgeItem = new ForgeItem(item, count, FORGE_ITEM_KEEP);
				_items[1] = forgeItem;
			} else if (isChanceType(item.getItemId())) { //幸运符
				forgeItem = new ForgeItem(item, count, FORGE_ITEM_CHANCE);
				_items[0] = forgeItem;
			}
		} else {
			forgeItem = new ForgeItem(item, count);
			for( int i=0; i < _limitSize; i++ )
			{
				
				if( _items[i] == null )
				{
					_items[i] = forgeItem;
					break;
				}
				
			}
		
		}

		_currentSize++;

		return forgeItem;

	}

	/**
	 * 是否 已有这个物品
	 * @param itemId
	 * @return
	 */
	protected boolean hasItem(long itemId) {
		for (ForgeItem item : _items) {
			if (item != null && item._objectId == itemId) {
				return true;
			}
		}

		return false;
	}

	/**
	 * 是否  是幸运符
	 * @param itemId
	 * @return
	 */
	protected boolean isChanceType(int itemId) {
		return (itemId == 30019 || itemId == 30020); //幸运符id
	}

	/**
	 * 移除锻造链表中的一个物品
	 * @param objectId
	 * @param count
	 * @return
	 */
	public ForgeItem removeItem(long objectId, int count) {
//		System.out.println("开始移除物品id:" + objectId + "物品数量" + count);
		int i = 0;
		for (ForgeItem item : _items) {
			if (item != null && item._objectId == objectId) {
				if (count > 0 && item._count > count) {
					item._count = item._count - count;
				} else {
					_items[i] = null;
				}

				_currentSize--;
//				System.out.println("移除的物品 :" + item);
				return item;

			}
			i++;

		}

		return null;
	}

}
